<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- import CSS -->
<link rel="stylesheet" href="/css/elementui/index.css">
<link rel="stylesheet" href="/css/exam/css/style.css">
<link rel="stylesheet" href="/iconfont/iconfont.css">
</head>
<body>
<div id="app">
  <div  class="p-30">
      <el-row>
      <span class="icon iconfont icon-arrow_back"></span> <a href="{{route('admin.examview.list')}}">返  回</a> | 新建试卷
      </el-row><br>
      <el-form :model="addExamForm" :rules="rules" ref="addExamForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="试卷名" prop="title" placeholder="试卷名">
                <el-input v-model="addExamForm.title"></el-input>
            </el-form-item>
            <el-form-item label="分&ensp;&ensp;类" prop="cid">
                <el-select v-model="addExamForm.cid" placeholder="请选择分类">
                    <el-option
                            v-for="item in cidData"
                            :key="item.id"
                            :label="item.p_name"
                            :value="item.id"
                        >
                    </el-option>
                </el-select> <el-button><a href="{{route('admin.examview.papercidlist')}}">分类管理 </a></el-button>
            </el-form-item>
            <el-form-item label="总&ensp;&ensp;分" prop="score" placeholder="总分">
                <el-input v-model="addExamForm.score"></el-input>
            </el-form-item>
            <el-form-item label="及格分" prop="pass_score" placeholder="及格分">
                <el-input v-model="addExamForm.pass_score"></el-input>
            </el-form-item>
            <el-form-item label="时&ensp;&ensp;间" prop="exam_duration" placeholder="单位：分钟">
                <el-input v-model="addExamForm.exam_duration"></el-input>
            </el-form-item>
            <el-form-item label="考试次数" prop="exam_num" placeholder="请输入整数">
                <el-input v-model="addExamForm.exam_num"></el-input>
            </el-form-item> 
            <el-row>
                <el-button @click="saveExam('addExamForm')" type="primary">保&ensp;&ensp;存</el-button>
                <el-button @click="cancelExam('addExamForm')">取&ensp;&ensp;消</el-button>
            </el-row>         
        </el-form>

  </div>

</div>
</body>
<script src="/css/exam/js/vue.js"></script>
<script src="/css/elementui/index.js"></script>
<script src="/css/exam/js/axios.min.js"></script>
<script>

const app = new Vue({
  el: '#app',
  data() {
    return { 
        addExamForm: {
        title: '',
        cid: '',
        pass_score: '',
        exam_duration: '',
        exam_num: '',
        score: '',
        token: localStorage.getItem('logintoken')
      },

        cidData: [], //分类数据

        rules: {
          title: [
            { required: true, message: '请输入试卷标题', trigger: 'blur' },
            { min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '请输入总分', trigger: 'blur' },
            { pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur' }
          ],
          pass_score: [
            { required: true, message: '请输入及格分', trigger: 'blur' },
            { pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur' }
          ],
          exam_duration: [
            { required: true, message: '单位：分钟', trigger: 'blur' },
            { pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur' }
          ],
          exam_num: [
            { required: true, message: '请输入整数', trigger: 'blur' },
            { pattern: /^[0-9]*$/, message: '请输入整数', trigger: 'blur' }
          ]
        },
    }
    
  },
  created() {
    this.getciddata();
  },
  methods: {
        // 编辑、增加页面保存方法
        saveExam(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

                let title = this.addExamForm.title;
                let cid = this.addExamForm.cid;
                let pass_score = this.addExamForm.pass_score;
                let score = this.addExamForm.score;
                let exam_duration = this.addExamForm.exam_duration;
                let exam_num = this.addExamForm.exam_num;
                axios.post('/admin/paper/edit',
                {title:title,cid:cid,pass_score:pass_score,exam_duration:exam_duration,exam_num:exam_num,score:score}
                )
                .then(res => {
                if (res.data.code == 0) {  
                    
                    this.$message({
                    type: 'success',
                    message: '保存成功！'
                    });
                   window.location=document.referrer;
                   this.$refs[formName].resetFields();
                }  else if(res.data.code == 408){
					this.$message({
						type: 'info',
						message: '该名称已存在，请重新录入！'
					})
				} else{
					this.$message({
						type: 'info',
						message: '网络原因，请稍后再试'
					})
				}
                })
          } else {
            return false;
          }
        });
      },
 
        cancelExam(formName) {
            this.$refs[formName].resetFields();
        },
        getciddata() {      
            axios.get('/admin/paper/category_index',{})
            .then(response =>{
                console.log('请求成功：',response.data.data);
                this.cidData = response.data.data;
                console.log(this.cidData);  
            }).catch(error =>{
                console.log('请求失败：' + error);
            })
          },
		  goBack() {
		    window.history.go(-1)
		    console.log('go back');
		  },

        },     
})

</script>
<style>
  a{color:#409EFF;text-decoration: none;}
</style>
</html>

